package cn.net.wanji.service.iml;

import cn.net.wanji.entity.HourVehicles;
import cn.net.wanji.entity.TrafficFlowVehicleDto;
import cn.net.wanji.entity.VehicleRsuInfo;
import cn.net.wanji.enums.CarType;
import cn.net.wanji.enums.CargoType;
import cn.net.wanji.enums.LabelType;
import cn.net.wanji.mapper.TrafficFlowStatisticsMapper;
import cn.net.wanji.service.TrafficFlowStatisticsService;
import cn.net.wanji.utils.StringUtils;
import cn.net.wanji.vo.VehicleExceptionVo;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

@Log4j2
@Service
public class TrafficFlowStatisticsImpl implements TrafficFlowStatisticsService {
    @Autowired
    private TrafficFlowStatisticsMapper trafficFlowStatisticsMapper;

    @Override
    public Map<String,List<TrafficFlowVehicleDto>> trafficFlowVehicleClassifiedStatistic(VehicleRsuInfo vehicleRsuInfo) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Date startTime = calendar.getTime();
        vehicleRsuInfo.setStartTime(startTime);
        List<TrafficFlowVehicleDto> trafficFlowVehicleDtos = trafficFlowStatisticsMapper.trafficFlowVehicleClassifiedStatistic(vehicleRsuInfo);
        List<TrafficFlowVehicleDto> buss = new ArrayList<>();
        List<TrafficFlowVehicleDto> trucks = new ArrayList<>();
        for (TrafficFlowVehicleDto trafficFlowVehicleDto : trafficFlowVehicleDtos) {
            trafficFlowVehicleDto.setName(trafficFlowVehicleDto.getCargoType(Integer.valueOf(trafficFlowVehicleDto.getName())).name());
            if (trafficFlowVehicleDto.getName().contains("客")) {
                buss.add(trafficFlowVehicleDto);
            }else {
                if (!trafficFlowVehicleDto.getName().contains("未知")){
                    trucks.add(trafficFlowVehicleDto);
                }
            }
        }
        Map<String,List<TrafficFlowVehicleDto>> hashMap = new HashMap<>();
        hashMap.put("bus", buss);
        hashMap.put("truck", trucks);
        return hashMap;
    }

    @Override
    public VehicleExceptionVo abnormalmodels(VehicleRsuInfo vehicleRsuInfo) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Date startTime = calendar.getTime();
        vehicleRsuInfo.setStartTime(startTime);
        VehicleRsuInfo abnormalmodels = trafficFlowStatisticsMapper.abnormalmodels(vehicleRsuInfo);
        if (StringUtils.isNull(abnormalmodels)) {
            return new VehicleExceptionVo();
        }
        VehicleExceptionVo vehicleExceptionVo = new VehicleExceptionVo();
        vehicleExceptionVo.setEtcVehClass(CarType.getNameFromId(abnormalmodels.getEtcVehClass()));
        vehicleExceptionVo.setHeadPicTime(abnormalmodels.getHeadPicTime());
        vehicleExceptionVo.setIdentifyType(CarType.getNameFromId(String.valueOf(abnormalmodels.getIdentifyType())));
        String licenseCode = abnormalmodels.getLicenseCode();
        if (licenseCode.contains("默")){
            licenseCode = abnormalmodels.getEtcLicense();
        }
        vehicleExceptionVo.setLicenseCode(licenseCode);
        vehicleExceptionVo.setSideImage(abnormalmodels.getSideImage());
        vehicleExceptionVo.setTailImage(abnormalmodels.getTailImage());
        vehicleExceptionVo.setLicenseImage(abnormalmodels.getLicenseImage());
        return vehicleExceptionVo;
    }
    @Override
    public VehicleExceptionVo labelAbnormality(VehicleRsuInfo vehicleRsuInfo) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Date startTime = calendar.getTime();
        vehicleRsuInfo.setStartTime(startTime);
        VehicleRsuInfo abnormalmodels = trafficFlowStatisticsMapper.labelAbnormality(vehicleRsuInfo);
        VehicleExceptionVo vehicleExceptionVo = new VehicleExceptionVo();
        if (StringUtils.isNull(abnormalmodels)) {
            return vehicleExceptionVo;
        }

        vehicleExceptionVo.setEtcVehClass(LabelType.getNameFromId(abnormalmodels.getUnlawfulMold()));
        vehicleExceptionVo.setHeadPicTime(abnormalmodels.getHeadPicTime());
        vehicleExceptionVo.setIdentifyType(CarType.getNameFromId(String.valueOf(abnormalmodels.getIdentifyType())));
        String licenseCode = abnormalmodels.getLicenseCode();
        if (licenseCode.contains("默")){
            licenseCode = abnormalmodels.getEtcLicense();
        }
        vehicleExceptionVo.setLicenseCode(licenseCode);
        vehicleExceptionVo.setSideImage(abnormalmodels.getSideImage());
        vehicleExceptionVo.setTailImage(abnormalmodels.getTailImage());
        vehicleExceptionVo.setLicenseImage(abnormalmodels.getLicenseImage());
        return vehicleExceptionVo;
    }

    @Override
    public List<VehicleRsuInfo> hazardousChemicals(VehicleRsuInfo vehicleRsuInfo) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Date startTime = calendar.getTime();
        vehicleRsuInfo.setStartTime(startTime);
        List<VehicleRsuInfo>  abnormalmodels = trafficFlowStatisticsMapper.hazardousChemicals(vehicleRsuInfo);
        for (VehicleRsuInfo abnormalmodel : abnormalmodels) {
            String licenseCode = abnormalmodel.getLicenseCode();
            if (licenseCode.contains("默")){
                licenseCode = abnormalmodel.getEtcLicense();
            }
            abnormalmodel.setLicenseCode(licenseCode);
            abnormalmodel.setLicenseColor(abnormalmodel.getCargoType(Integer.parseInt(abnormalmodel.getLicenseColor())).name());
            if (abnormalmodel.getDangerousGoods().contains("危化")){
                abnormalmodel.setSpecialClass("危化品车");
            }
            if (abnormalmodel.getIdentifyType() == 3 || abnormalmodel.getIdentifyType() == 4){
                abnormalmodel.setSpecialClass(CarType.getNameFromId(String.valueOf(abnormalmodel.getIdentifyType())));
            }
            abnormalmodel.setHeadLaneNum(abnormalmodel.getHeadLaneNum());
        }
        return abnormalmodels;
    }

    @Override
    public Map<String,Long> vehicleRsuInfos(VehicleRsuInfo vehicleRsuInfo) {


        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Date startTime = calendar.getTime();
        vehicleRsuInfo.setStartTime(startTime);

        Map<String, Long> stringLongHashMap = new HashMap<>();
        Long aLong = trafficFlowStatisticsMapper.vehicleRsuInfoTotals(vehicleRsuInfo);

        Long beforeHourTotals = trafficFlowStatisticsMapper.vehicleRsuInfoBeforeHourTotals();
        Long errTotals = trafficFlowStatisticsMapper.vehicleRsuInfoErrTotals(vehicleRsuInfo);
        Long modelTotals = trafficFlowStatisticsMapper.inconsistentModelTotals(vehicleRsuInfo);
        Long hazardousChemicalTotals = trafficFlowStatisticsMapper.hazardousChemicalTotals(vehicleRsuInfo);
        stringLongHashMap.put("vehicleRsuInfoTotals", aLong);
        stringLongHashMap.put("beforeHourTotals", beforeHourTotals);
        stringLongHashMap.put("errTotals", errTotals);
        stringLongHashMap.put("modelTotals", modelTotals);
        stringLongHashMap.put("hazardousChemicalTotals", hazardousChemicalTotals);
        return stringLongHashMap;
    }

    @Override
    public List<Map<String, String>> getFlowStatementList(VehicleRsuInfo vehicleRsuInfo) throws IllegalAccessException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Date startTime = calendar.getTime();
        vehicleRsuInfo.setStartTime(startTime);
        HourVehicles flowStatementList = trafficFlowStatisticsMapper.getFlowStatementList(vehicleRsuInfo);

        List<Map<String, String>> maps = new ArrayList<>();
        Class<HourVehicles> hourVehiclesClass = HourVehicles.class;
        Field[] fields = hourVehiclesClass.getDeclaredFields();
        for (Field field : fields) {
            Map<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("name", field.getName());
            field.setAccessible(true);
            Object value = field.get(flowStatementList);
            objectObjectHashMap.put("num", value.toString());
            maps.add(objectObjectHashMap);
        }
        return maps;
    }
}
